package com.bird.multipleevent;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Arrays;
import java.util.List;

/**
 * @data 2023/4/8 11:23
 * @author: bird
 * @description:
 * 发布事件
 */
public class AccidentPublishEvent {

    public static void main(String[] args) {
        //创建容器
        AnnotationConfigApplicationContext applicationContext=new AnnotationConfigApplicationContext();
       // 添加3中不同监听器
        applicationContext.addApplicationListener(new RedAccidentEventListener());
        applicationContext.addApplicationListener(new OrangeAccidentEventListener());
        applicationContext.addApplicationListener(new YellowAccidentEventListener());
        //刷新容器
        applicationContext.refresh();

    // 开始处理业务逻辑
        List<String> colorList= Arrays.asList("red","orange","yellow");

        for (String color : colorList) {
            AccidentEvent event = null;
            if(color.equals("red")){
                  event = createRedEvent(color);
            }else if(color.equals("orange")){
                event = createOrangeEvent(color);
            }else if(color.equals("yellow")){
                event = createYellowEvent(color);
            }

            applicationContext.publishEvent(event);
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }


    }

    private static AccidentEvent createRedEvent(String color) {
        AccidentDTO accidentDTO = new AccidentDTO();
        accidentDTO.setNumber(color);
        return new RedAccidentEvent(accidentDTO);
    }

    private static AccidentEvent createOrangeEvent(String color) {
        AccidentDTO accidentDTO = new AccidentDTO();
        accidentDTO.setNumber(color);
        return new OrangeAccidentEvent(accidentDTO);
    }
    private static AccidentEvent createYellowEvent(String color) {
        AccidentDTO accidentDTO = new AccidentDTO();
        accidentDTO.setNumber(color);
        return new YellowAccidentEvent(accidentDTO);
    }

}
